	package com.example.ten_minutes_more;

import java.util.ArrayList;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class Setting_adapter extends BaseAdapter {
	Context context;
	ArrayList<Alarm_data> data;
	LayoutInflater inflater;
	static Database db;
	
	public Setting_adapter(Context c, ArrayList<Alarm_data> data){
		this.context = c;
		db = new Database(c);
		db.open();
		this.data = data;
		inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position).getTime();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null)
			convertView = inflater.inflate(R.layout.list_layout, parent, false);
	    	
	    	
		if(data.get(position) != null){
			ImageView clock = (ImageView)convertView.findViewById(R.id.clock);
			clock.setImageResource(data.get(position).getClock());
		
			final TextView time = (TextView)convertView.findViewById(R.id.time);
			time.setText(data.get(position).getTime());
			
			final ImageView monday = (ImageView)convertView.findViewById(R.id.monday);
			monday.setImageResource(data.get(position).getDay_mon());
			
			final ImageView tuesday = (ImageView)convertView.findViewById(R.id.tuesday);
			tuesday.setImageResource(data.get(position).getDay_tue());
			
			final ImageView wednesday = (ImageView)convertView.findViewById(R.id.wednesday);
			wednesday.setImageResource(data.get(position).getDay_wed());
			
			final ImageView thursday = (ImageView)convertView.findViewById(R.id.thursday);
			thursday.setImageResource(data.get(position).getDay_thu());
			
			final ImageView friday = (ImageView)convertView.findViewById(R.id.friday);
			friday.setImageResource(data.get(position).getDay_fri());
			
			final ImageView saturday = (ImageView)convertView.findViewById(R.id.saturday);
			saturday.setImageResource(data.get(position).getDay_sat());
			
			final ImageView sunday = (ImageView)convertView.findViewById(R.id.sunday);
			sunday.setImageResource(data.get(position).getDay_sun());
			
			//final ToggleButton on_off = (ToggleButton)convertView.findViewById(R.id.on_off);
			ImageView delete = (ImageView)convertView.findViewById(R.id.delete);
			delete.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					int id = data.get(position).getId();
					//Setting_alarm.cancelAlarm(data.get(position).getId());
					
					 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
					 Intent intent = new Intent(context, PopupActivity.class);
					 PendingIntent pi = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
					 alarmManager.cancel(pi);
					    
					Toast.makeText(context, "알람이 해제되었습니다",Toast.LENGTH_SHORT).show();
					
					
					db.deleteAlarmList(data.get(position).getId());
					MainActivity.data.remove(position);
					MainActivity.adapter.notifyDataSetChanged();
				}
				
			});
			
			final TextView am_pm = (TextView)convertView.findViewById(R.id.am_pm);
			am_pm.setText(data.get(position).getAm_Pm());
			
			convertView.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Setting_alarm.ISNEW = false;
					Setting_alarm.POSITION = position;
					Intent info = new Intent(context, Setting_alarm.class);
					context.startActivity(info);
				}
			});
			
			//convertView.setOnCreateContextMenuListener(MainActivity.contextmenu);
			
			
		}
		return convertView; 
	}
	/*
	public static void deleteAlarm(MenuItem item){
		
		  AdapterView.AdapterContextMenuInfo info = 
                  (AdapterView.AdapterContextMenuInfo) item.getOrder(); 
		
		Alarm_data selected = MainActivity.data.get(info.position); 
    	int id = selected.getId();
    	
    	db.deleteAlarmList(id);
    	MainActivity.data.clear();
    	MainActivity.setData();
    	MainActivity.adapter.notifyDataSetChanged();
		
	}
	*/

}
